import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.xml.bind.Element;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {

    public static void main(String[] args) throws InterruptedException, IOException {
//        test01();
//        test02();
        // 这个test03会报错，因为点击的是百度新闻超链接
        // 这个超链接没有放到form标签中
//        test03();
//        test04();
//        test05();
//        test06();
//        test07();
//        test08();
//        test09();
//        page01();
//        page02();
//        page03();
//        page04();
//        page05();
//        test10();
//        test11();
        test12();
    }

    private static void test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://20230521jietu.png"));
    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles获取所有的窗口句柄
        // 通过getWindowHandle获取的get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:handles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(4000);
        webDriver.close();
    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/Test_20230525_testcode/src/main/Page/test05.html?_ijt=k24j3jvorcqfvmokm7bfjob082&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\untitled");
    }


    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/Test_20230525_testcode/src/main/Page/test04.html?_ijt=pativuf1mgq7ujraaef00rhj6s&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        // alert 弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        // 点击按钮
        webDriver.findElement(By.cssSelector("button")).click();
        // 在alert弹窗中输入赵彦博
        webDriver.switchTo().alert().sendKeys("赵彦博");
        // alert弹窗却仍
        sleep(3000);
        webDriver.switchTo().alert().accept();
    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/Test_20230525_testcode/src/main/Page/test03.html?_ijt=qs8m1guauu36851rbc8f87rmpj&_ij_reload=RELOAD_ON_SAVE");
        WebElement webElement = webDriver.findElement(By.cssSelector("ShippingMethod"));
        Select select = new Select(webElement);
        select.selectByIndex(3);
        select.selectByValue("12.51");
    }

    private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/Test_20230525_testcode/src/main/Page/test02.html?_ijt=tnr43r6b3ls2c0gh7sh0ef6utt&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
//        webDriver.findElement(By.cssSelector("body > div > div > a")).click();

    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/Test_20230525_testcode/src/main/Page/test01.html?_ijt=v2sku8114sanap1r1e8l53eli4&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for(int i = 0; i < webElements.size(); i++) {
            // 如果每个元素type值等于checkbox进行点击
            // getAttribute获取页面上的元素属性值，里面的type是当前元素属性
            if(webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            } else {
                // 否则什么也不操作
                ;
            }
        }
    }

    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 找到图片按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-zhidao_cTR5H.s-tab-zhidao"));
        // 鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(webElement).contextClick().perform();

    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        // control+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        // control+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        // control+V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
        sleep(3000);
    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        // 强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        // 浏览器后退
        sleep(3000);
        webDriver.navigate().back();
        sleep(3000);
        // 强制等待3秒
        webDriver.navigate().refresh();
        // 浏览器前进
        sleep(3000);
        webDriver.navigate().forward();
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeAsyncScript("document.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }


    private static void test06() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver, 3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div111")));

    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if (url.equals("https://www.baidu.com") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url:" + url + ",当前页面title:" + title);
            System.out.println("测试通过");
        }  else {
            System.out.println("测试不通过");
        }
    }


    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (button_value.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();

    }


    public static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        // 找到百度搜索输入框，输入java107
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("java107");
        // 点击了百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).submit();
//        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        // 清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    public static void test01() throws InterruptedException {
        int flag = 0;
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 找到百度搜索输入框
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        // 输入软件测试
        element.sendKeys("软件测试");
        // 找到百度一下按钮
        // 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 校验
        // 找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
//            System.out.println(elements.get(i).getText());
            if (elements.get(i).getText().equals("测试")) {
                flag = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if (flag == 0) {
            System.out.println("测试不通过");
        }

    }
}